import {messages, queryCurrencyPrecision} from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'
import { Form, Icon, message } from 'antd'

class LoanRelatedApplication extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      requestDetail: menuRoute.getRouteItem('request-detail','key'), //申请单详情页
      loanDetail: menuRoute.getRouteItem('loan-detail','key') //借款单详情页
    }
  }

  toRelatedApplication = () => {
    const { info } = this.props;
    const { requestDetail } = this.state;
    let reference = info.referenceApplication;
    let url = requestDetail.url.replace(':formOID', reference.formOID).replace(':applicationOID', reference.applicationOID) + '?from=request';
    if (window.ISIE9) {
      url = `#${url}`;
    }
    window.open(url);
  };

  toGoLoanDetail = () => {
    const { info } = this.props;
    const { loanDetail } = this.state;
    let applicationOID = info.applicationOID;
    let loanBillOID = info && info.loanBillOID;
    let loanFormOID = info && info.loanBill && info.loanBill.formOID;
    let loanStatus = info && info.loanBill && info.loanBill.status;
    if (loanStatus === 1010) {
      message.error(messages('request.key570')/*未找到借款单，单据可能已被删除。*/);
      return;
    }
    let url = loanDetail.url.replace(':formOID', loanFormOID).replace(':loanBillOID', loanBillOID).replace(':applicationOID', applicationOID);
    if (window.ISIE9) {
      url = `#${url}`;
    }
    window.open(url);
  };

  render() {
    const { info, isAutoLoan } = this.props;
    let currencyCode = info && info.loanBill && info.loanBill.currencyCode;
    let currencyAmount = info && info.loanBill && info.loanBill.currencyAmount;
    return (
      <div className="loan-related-application">
        {info.referenceApplication && info.referenceApplication.businessCode && !isAutoLoan && (
          <div className="row-container">
            <Icon type="link" className="link-icon"/>
            <a onClick={this.toRelatedApplication}>{messages('request.key117'/*申请单*/)}：{info.referenceApplication.businessCode}</a>
          </div>
        )}
        {isAutoLoan && (
          <div className="row-container">
            <span>{messages('request.key616')/*创建借款*/}：</span>
            <Icon type="link" className="link-icon"/>
            <a onClick={this.toGoLoanDetail}>{info.loanBill.businessCode}</a>
            <span style={{color: '#666'}}>
                &nbsp;&nbsp;&nbsp;{currencyCode}&nbsp;{React.Component.prototype.filterMoney(currencyAmount, queryCurrencyPrecision(currencyCode), true)}
            </span>
          </div>
        )}
      </div>
    )
  }
}

LoanRelatedApplication.propTypes = {
  info: React.PropTypes.object,
  isAutoLoan: React.PropTypes.bool
};

LoanRelatedApplication.defaultProps={
  info: {},
  isAutoLoan: false
};

LoanRelatedApplication.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps() {
  return { }
}

const wrappedLoanRelatedApplication = Form.create()(LoanRelatedApplication);

export default connect(mapStateToProps)(wrappedLoanRelatedApplication)
